package com.inplaceedit.client.ui;

import java.util.Iterator;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiChild;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasOneWidget;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * A decorator that allows you to place commands (e.g. "edit") to the right of a
 * widget.
 * 
 */
public class CommandPanel extends Composite implements HasWidgets {

	interface Binder extends UiBinder<Widget, CommandPanel> {
	}

	private static Binder uiBinder = GWT.create(Binder.class);

	@UiField
	SimplePanel contentPanel;
	@UiField
	SimplePanel commandPanel;

	public CommandPanel() {
		initWidget(uiBinder.createAndBindUi(this));
	}

	@Override
	@UiChild(limit = 1)
	public void add(final Widget w) {
		contentPanel.add(w);
	}

	@Override
	public void clear() {
		contentPanel.clear();
	}

	public HasOneWidget getCommandPanel() {
		return commandPanel;
	}

	public HasOneWidget getContentPanel() {
		return contentPanel;
	}

	@Override
	public Iterator<Widget> iterator() {
		return contentPanel.iterator();
	}

	@Override
	public boolean remove(final Widget w) {
		return contentPanel.remove(w);
	}

	@UiChild(limit = 1, tagname = "commands")
	public void setCommandWidget(final Widget w) {
		commandPanel.setWidget(w);
	}
}
